package fr.azzale.casu.utils;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import fr.azzale.casu.activity.TribuneActivity;

public class AuthCasuWebViewClient extends WebViewClient {

	private Context ctx;

	public AuthCasuWebViewClient(Context ctx) {
		super();
		this.ctx = ctx;
	}
//http://www.casugaming.com/index.php?action=login2%3bsa=check%3bmember=363
	@Override
	public void onPageStarted(WebView view, String url, Bitmap favicon) {
		CookieManager mgr = CookieManager.getInstance();
		Log.i( "URL", url );
		Log.i("Cookie","got cookie: "+mgr.getCookie("www.casugaming.com"));
		String cookie_string = mgr.getCookie("www.casugaming.com");
		if(cookie_string!=null && cookie_string.length() > 1 && cookie_string.contains("SMF"))
		{
			Editor edit = ctx.getSharedPreferences("Casu", Context.MODE_PRIVATE).edit();
			edit.putString("cookie",cookie_string);
			edit.commit();
			Intent i = new Intent(ctx, TribuneActivity.class);
			i.putExtra("cookie", cookie_string);
			ctx.startActivity(i);
		}
		else
			super.onPageStarted(view, url, favicon);
	}
	@Override
	public void onPageFinished(WebView view, String url) {
		//view.loadUrl("javascript:window.HTMLOUT.showHTML(document.getElementsByTagName('html')[0].innerHTML);");  
		super.onPageFinished(view, url);
	}
	
	
}
